<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\models\UserBalance;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Award
{
    protected $user;
    protected $order;
    // protected $set;

    protected $items;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $order->user_id]);
        $this->set = SetUserUp::getLastOne();

        // item list
        $this->items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->all();

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 报单区才有奖
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {
            $this->recommend();

            $this->team();
            $this->profit();

            $this->leader();
        }

        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPEAT'])) {

            $this->diff();

            $this->team();
            $this->profit();

            $this->leader();
        }
    }

    // 推荐奖
    protected function recommend()
    {
        $total_price = $this->order->total_pay - $this->order->total_freight;
        $r = Chain::getReferrer($this->order->user_id, true);
        if (!$r) {
            return;
        }

        $amount1 = (float)UserLevel::find()
            ->where(['value' => UserLevel::$keys['vip1']])
            ->select(['price'])
            ->scalar();
        $amount2 = (float)UserLevel::find()
            ->where(['value' => UserLevel::$keys['vip2']])
            ->select(['price'])
            ->scalar();
        $amount3 = (float)UserLevel::find()
            ->where(['value' => UserLevel::$keys['vip3']])
            ->select(['price'])
            ->scalar();
        $amount4 = (float)UserLevel::find()
            ->where(['value' => UserLevel::$keys['vip4']])
            ->select(['price'])
            ->scalar();

        if ($total_price == $amount1) {
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $total_price * Param::$c_vip1;
            }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $total_price * Param::$c_vip2;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $total_price * Param::$c_vip3;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $total_price * Param::$c_vip4;
            }

            // 直推, 平级
            $this->saveCommission($r->id, $commission, '100', '直推');
            // $this->equal($r, $commission);
        }

        if ($total_price == $amount2) {
            if ($r->level < UserLevel::$keys['vip2']) {
                $commission = $total_price * Param::$c_vip2 * Param::$low;
                $this->saveCommission($r->id, $commission, '100', '直推');
                $this->low($r, $commission, [
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4']
                ]);
            } else {
                if ($r->level == UserLevel::$keys['vip2']) {
                    $commission = $total_price * Param::$c_vip2;
                }
                if ($r->level == UserLevel::$keys['vip3']) {
                    $commission = $total_price * Param::$c_vip3;
                }
                if ($r->level == UserLevel::$keys['vip4']) {
                    $commission = $total_price * Param::$c_vip4;
                }

                $this->saveCommission($r->id, $commission, '100', '直推');
                // $this->equal($r, $commission);
            }
        }

        if ($total_price == $amount3) {
            if ($r->level < UserLevel::$keys['vip3']) {
                $commission = $total_price * Param::$c_vip3 * Param::$low;
                $this->saveCommission($r->id, $commission, '100', '直推');
                $this->low($r, $commission, [
                    UserLevel::$keys['vip4']
                ]);
            } else {
                if ($r->level == UserLevel::$keys['vip3']) {
                    $commission = $total_price * Param::$c_vip3;
                }
                if ($r->level == UserLevel::$keys['vip4']) {
                    $commission = $total_price * Param::$c_vip4;
                }

                $this->saveCommission($r->id, $commission, '100', '直推');
                // $this->equal($r, $commission);
            }
        }

        if ($total_price == $amount4) {
            if ($r->level < UserLevel::$keys['vip4']) {
                $commission = $total_price * Param::$c_vip4 * Param::$low;
                $this->saveCommission($r->id, $commission, '100', '直推');
            } else {
                if ($r->level == UserLevel::$keys['vip4']) {
                    $commission = $total_price * Param::$c_vip4;
                }

                $this->saveCommission($r->id, $commission, '100', '直推');
                // $this->equal($r, $commission);
            }
        }
    }

    // 领导奖(平级奖)
    private function leader()
    {
        $award_name = '领导奖';
        $award_type = 50;

        $vip2 = 0;
        $vip3 = 0;
        $vip4 = 0;

        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;
        // $has_vip4_2 = false;

        $chain = Chain::getReferrerChain($this->order->user_id, [], true);

        foreach ($chain as $user) {
            if ($user->level == UserLevel::$keys['vip1']) {
                continue;
            }

            if ((!$has_vip2 && !$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip2']) {
                if ($vip2 == 0) {
                    $vip2 = $this->_leader($user->id) * Param::$e_vip2;
                } else {
                    $this->saveCommission($user->id, $vip2, $award_type, $award_name);
                    $has_vip2 = true;
                }
            }
            if ((!$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip3']) {
                if ($vip3 == 0) {
                    $vip3 = $this->_leader($user->id) * Param::$e_vip3;
                } else {
                    $this->saveCommission($user->id, $vip3, $award_type, $award_name);
                    $has_vip3 = true;
                }
            }
            if ((!$has_vip4) && $user->level == UserLevel::$keys['vip4']) {
                if ($vip4 == 0) {
                    $vip4 = $this->_leader($user->id) * Param::$e_vip4;
                } else {
                    $this->saveCommission($user->id, $vip4, $award_type, $award_name);
                    $has_vip4 = true;

                    // 第二代
                    $r2 = Chain::getReferrerChainOne($user->id, [UserLevel::$keys['vip4']], true);
                    if ($r2) {
                        $this->saveCommission($r2->id, $vip4 * Param::$e_vip4, $award_type, $award_name);
                    }
                    // $has_vip4_2 = true;
                    break;
                }
            }
        }
    }

    private function _leader($user_id)
    {
        // 获取全部收益
        $total = UserCommission::find()
            ->where(['user_id' => $user_id])
            ->andWhere(['order_id' => $this->order->id])
            // ->andWhere(['award_type' => [100, 40, 200, 30, 31]])
            ->sum('balance');

        return floatval($total);
    }

    // 平级奖[不用了]
    private function equal($user, $commission)
    {
        $r = Chain::getReferrer($user->id, true);
        // if ($r && $r->level == $user->level) {
        if ($r && $r->level >= $user->level) {
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $commission * Param::$e_vip2;
                $this->saveCommission($r->id, $commission, '101', '领导奖');
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $commission * Param::$e_vip3;
                $this->saveCommission($r->id, $commission, '101', '领导奖');
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $commission * Param::$e_vip4;
                $this->saveCommission($r->id, $commission, '101', '领导奖');

                // 返2层
                $r2 = Chain::getReferrer($r->id, true);
                // if ($r2 && $r2->level == $r->level) {
                if ($r2 && $r2->level >= $r->level) {
                    $commission = $commission * Param::$e_vip4;
                    $this->saveCommission($r->id, $commission, '102', '领导奖');
                }
            }
        }
    }

    // 低推高, 另一半给高级
    private function low($user, $commission, $levels = [])
    {
        $r = Chain::getReferrerChainOne($user->id, $levels);
        if ($r) {
            $this->saveCommission($r->id, $commission, '50', '直推');
        }
    }

    // 复购奖
    protected function diff()
    {
        $award_name = '差价';
        $award_type = 40;

        $diff_vip1 = 0;
        $diff_vip2 = 0;
        $diff_vip3 = 0;
        $diff_vip4 = 0;

        $has_vip1 = false;
        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;

        $chain = Chain::getReferrerChain($this->order->user_id, [], true);

        foreach ($chain as $user) {
            if ((!$has_vip1 && !$has_vip2 && !$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip1']) {
                $diff_vip1 = $this->_diff('diff_vip1');
                $this->saveCommission($user->id, $diff_vip1, $award_type, $award_name);
                $has_vip1 = true;
            }
            if ((!$has_vip2 && !$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip2']) {
                $diff_vip2 = $this->_diff('diff_vip2');
                $diff_vip2 -= $diff_vip1;
                // var_dump($diff_vip2);exit;
                $this->saveCommission($user->id, $diff_vip2, $award_type, $award_name);
                $has_vip2 = true;
            }
            if ((!$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $this->_diff('diff_vip3');
                $diff_vip3 -= $diff_vip2 + $diff_vip1;
                $this->saveCommission($user->id, $diff_vip3, $award_type, $award_name);
                $has_vip3 = true;
            }
            if ((!$has_vip4) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $this->_diff('diff_vip4');
                $diff_vip4 -= $diff_vip3 + $diff_vip2 + $diff_vip1;
                $this->saveCommission($user->id, $diff_vip4, $award_type, $award_name);
                $has_vip4 = true;
                break;
            }
        }
    }

    private function _diff($diff_key)
    {
        $order_items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->all();

        $sum = 0;
        foreach ($order_items as $item) {
            $product = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();
            $sum += ($item['price'] - $product[$diff_key]) * $item['quantity'];
        }

        return $sum;
    }

    // 团队奖
    protected function team()
    {
        $total_price = $this->order->total_pay - $this->order->total_freight;
        $award_name = '团队奖';
        $award_type = 200;

        $diff_vip2 = 0;
        $diff_vip3 = 0;
        $diff_vip4 = 0;

        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;

        $chain = Chain::getReferrerChain($this->order->user_id, [], true);

        foreach ($chain as $user) {
            if ((!$has_vip2 && !$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip2']) {
                $diff_vip2 = $total_price * Param::$t_vip2;
                $this->saveCommission($user->id, $diff_vip2, $award_type, $award_name);
                $has_vip2 = true;
            }
            if ((!$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $total_price * Param::$t_vip3;
                $diff_vip3 -= $diff_vip2;
                $this->saveCommission($user->id, $diff_vip3, $award_type, $award_name);
                $has_vip3 = true;
            }
            if ((!$has_vip4) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $total_price * Param::$t_vip4;
                $diff_vip4 -= $diff_vip3 + $diff_vip2;
                $this->saveCommission($user->id, $diff_vip4, $award_type, $award_name);
                $has_vip4 = true;
                break;
            }
        }
    }

    // 分红
    protected function profit()
    {
        $total_price = $this->order->total_pay - $this->order->total_freight;

        $vip3 = Chain::getReferrerChain($this->order->user_id, [UserLevel::$keys['vip3']]);
        $vip4 = Chain::getReferrerChain($this->order->user_id, [UserLevel::$keys['vip4']]);

        if ($vip3) {
            $commission = $total_price * Param::$p_vip3;
            $commission /= count($vip3);
            foreach ($vip3 as $user) {
                $this->saveCommission($user['id'], $commission, 30, '分红');
            }
        }

        if ($vip4) {
            $commission = $total_price * Param::$p_vip4;
            $commission /= count($vip4);
            foreach ($vip4 as $user) {
                $this->saveCommission($user['id'], $commission, 31, '分红');
            }
        }

    }

    // 新推荐奖
    // protected function a815()
    // {
    //     $total_price = $this->order->total_pay - $this->order->total_freight;
    //     $level = UserLevel::find()
    //         ->where(['>=', 'price', $total_price])
    //         ->one();
    //
    //     if ($level) {
    //         $r = Chain::getReferrer($this->order->user_id, true);
    //         if ($r) {
    //
    //             if ($r->level < $level->value) {
    //
    //                 // 低推高
    //                 $this->low815($level, $r);
    //             } else {
    //
    //                 // 正常2+1
    //
    //                 // 推董事
    //                 if ($level->value == UserLevel::$keys['vip6']) {
    //                     if ($r->v5 >= 1) {
    //
    //                         // 直推
    //                         $commission = Param::$vip6_1 + Param::$vip6_2;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 平级奖
    //                         $this->equal815($commission, $r, 'v5');
    //                     } else {
    //
    //                         // 直推
    //                         $commission = Param::$vip6_1;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 间推
    //                         $r2 = $this->getIndirect($r->id, 'v5', UserLevel::$keys['vip6']);
    //                         if ($r2) {
    //                             $commission = Param::$vip6_2;
    //                             $this->saveCommission($r2->id, $this->order->user_id, $this->order->id, $commission, '12', '间推');
    //
    //                             // 平级奖
    //                             $this->equal815($commission, $r2, 'v5');
    //                         }
    //                     }
    //                 }
    //
    //                 // 推联创
    //                 if ($level->value == UserLevel::$keys['vip5']) {
    //                     if ($r->v4 >= 1) {
    //
    //                         // 直推
    //                         $commission = Param::$vip5_1 + Param::$vip5_2;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 平级奖
    //                         $this->equal815($commission, $r, 'v4');
    //                     } else {
    //
    //                         // 直推
    //                         $commission = Param::$vip5_1;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 间推
    //                         $r2 = $this->getIndirect($r->id, 'v4', UserLevel::$keys['vip5']);
    //                         if ($r2) {
    //                             $commission = Param::$vip5_2;
    //                             $this->saveCommission($r2->id, $this->order->user_id, $this->order->id, $commission, '12', '间推');
    //
    //                             // 平级奖
    //                             $this->equal815($commission, $r2, 'v4');
    //                         }
    //                     }
    //                 }
    //
    //                 // 推经销商
    //                 if ($level->value == UserLevel::$keys['vip4']) {
    //                     if ($r->v3 >= 1) {
    //
    //                         // 直推
    //                         $commission = Param::$vip4_1 + Param::$vip4_2;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 平级奖
    //                         $this->equal815($commission, $r, 'v3');
    //                     } else {
    //
    //                         // 直推
    //                         $commission = Param::$vip4_1;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 间推
    //                         $r2 = $this->getIndirect($r->id, 'v3', UserLevel::$keys['vip4']);
    //                         if ($r2) {
    //                             $commission = Param::$vip4_2;
    //                             $this->saveCommission($r2->id, $this->order->user_id, $this->order->id, $commission, '12', '间推');
    //
    //                             // 平级奖
    //                             $this->equal815($commission, $r2, 'v3');
    //                         }
    //                     }
    //                 }
    //
    //                 // 推创客
    //                 if ($level->value == UserLevel::$keys['vip3']) {
    //                     if ($r->v2 >= 1) {
    //
    //                         // 直推
    //                         $commission = Param::$vip3_1 + Param::$vip3_2;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 平级奖
    //                         $this->equal815($commission, $r, 'v2');
    //                     } else {
    //
    //                         // 直推
    //                         $commission = Param::$vip3_1;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 间推
    //                         $r2 = $this->getIndirect($r->id, 'v2', UserLevel::$keys['vip3']);
    //                         if ($r2) {
    //                             $commission = Param::$vip3_2;
    //                             $this->saveCommission($r2->id, $this->order->user_id, $this->order->id, $commission, '12', '间推');
    //
    //                             // 平级奖
    //                             $this->equal815($commission, $r2, 'v2');
    //                         }
    //                     }
    //                 }
    //
    //                 // 推荐会员, 我估计这里是普通会员
    //                 if ($level->value == UserLevel::$keys['vip1']) {
    //                     if ($r->v1 >= 1) {
    //
    //                         // 直推
    //                         $commission = Param::$vip1_1 + Param::$vip1_2;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 平级奖
    //                         $this->equal815($commission, $r, 'v1');
    //                     } else {
    //
    //                         // 直推
    //                         $commission = Param::$vip1_1;
    //                         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '11', '直推');
    //
    //                         // 间推
    //                         // 这里一定是VIP2, VIP1
    //                         $r2 = $this->getIndirect($r->id, 'v1', [UserLevel::$keys['vip1'], UserLevel::$keys['vip2']]);
    //                         if ($r2) {
    //                             $commission = Param::$vip1_2;
    //                             $this->saveCommission($r2->id, $this->order->user_id, $this->order->id, $commission, '12', '间推');
    //
    //                             // 平级奖
    //                             $this->equal815($commission, $r2, 'v1');
    //                         }
    //                     }
    //                 }
    //
    //             }
    //
    //         }
    //     }
    // }

    // 新平级奖
    // protected function equal815($commission, $user, $vip)
    // {
    //     $r = Chain::getReferrer($user['id'], true);
    //     if ($r && $r[$vip] == $user[$vip]) {
    //         $commission = $commission * Param::$equal;
    //         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '13', '平级');
    //     }
    // }

    // 低推高
    // protected function low815($level, $r)
    // {
    //     if ($r->level < UserLevel::$keys['vip2']) {
    //         return;
    //     }
    //
    //     $commission = 0;
    //
    //     // 推董事
    //     if ($level->value == UserLevel::$keys['vip6']) {
    //         $commission = Param::$vip6_low;
    //     }
    //
    //     // 推联创
    //     if ($level->value == UserLevel::$keys['vip5']) {
    //         $commission = Param::$vip5_low;
    //     }
    //
    //     // 推经销商
    //     if ($level->value == UserLevel::$keys['vip4']) {
    //         $commission = Param::$vip4_low;
    //     }
    //
    //     // 推创客
    //     if ($level->value == UserLevel::$keys['vip3']) {
    //         $commission = Param::$vip3_low;
    //     }
    //
    //     if ($commission > 0) {
    //         $this->saveCommission($r->id, $this->order->user_id, $this->order->id, $commission, '10', '推荐');
    //     }
    // }

    // 高级截断低级
    // private function hasVipHigh($the_referrer, $vip)
    // {
    //     // return false;
    //
    //     if ($vip == 'v1') {
    //         if ($the_referrer['v2'] > 0 || $the_referrer['v3'] > 0 || $the_referrer['v4'] > 0 || $the_referrer['v5'] > 0) {
    //             return $the_referrer;
    //         }
    //     }
    //     if ($vip == 'v2') {
    //         if ($the_referrer['v3'] > 0 || $the_referrer['v4'] > 0 || $the_referrer['v5'] > 0) {
    //             return $the_referrer;
    //         }
    //     }
    //     if ($vip == 'v3') {
    //         if ($the_referrer['v4'] > 0 || $the_referrer['v5'] > 0) {
    //             return $the_referrer;
    //         }
    //     }
    //     if ($vip == 'v4') {
    //         if ($the_referrer['v5'] > 0) {
    //             return $the_referrer;
    //         }
    //     }
    //
    //     return false;
    // }

    // 2 + 1 间推
    // 根据VIP来的
    // private function getIndirect($user_id, $vip = 'v1', $level = '202')
    // {
    //     $the_referrer = Chain::getReferrer($user_id, true);
    //     if (!$the_referrer) {
    //         return null;
    //     }
    //
    //     // var_dump($the_referrer['id'], $vip);exit;
    //     $has = $this->hasVipHigh($the_referrer, $vip);
    //     if ($has) {
    //         return $has;
    //     }
    //
    //     // 处理两线条
    //     if ($the_referrer[$vip] > 0) {
    //
    //         // 判断是否属于前2个直推
    //         $children = User::find()
    //             ->where(['referrer_id' => $the_referrer->id])
    //             // 应该至少是会员(普通会员)
    //             // ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
    //             // 对应等级
    //             ->andWhere(['level' => $level])
    //             ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
    //             ->select(['id'])
    //             ->column();
    //         // var_dump($children);exit;
    //
    //         if (count($children) > Param::$line) {
    //             $children = [$children[0], $children[1]];
    //             $in = in_array($user_id, $children);
    //             if (!$in) {
    //                 return $the_referrer;
    //             } else {
    //                 return $this->getIndirect($the_referrer->id, $vip, $level);
    //             }
    //         } else {
    //             return $this->getIndirect($the_referrer->id, $vip, $level);
    //         }
    //     }
    //
    //     return $this->getIndirect($the_referrer->id, $vip, $level);
    // }

    // 分红
    // protected function goSell()
    // {
    //     // 销售分红 (每笔订单进来, 都需要处理整个推荐人关系链)
    //     $chain = Chain::getReferrerChain($this->order->user_id, [
    //         UserLevel::$keys['vip2'],
    //         UserLevel::$keys['vip3'],
    //         UserLevel::$keys['vip4']
    //     ], true);
    //     foreach ($chain as $item) {
    //         $this->sell($item);
    //     }
    // }

    // 获取差价
    // (低级 - 高级 )价格 * 盒数 * 数量
    // private function getDiffPrice($high_key, $low_key)
    // {
    //     $order_items = OrderItem::find()
    //         ->where(['order_id' => $this->order->id])
    //         ->all();
    //
    //     $sum = 0;
    //     foreach ($order_items as $item) {
    //         $set = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();
    //         $unit = Product::find()
    //             ->where(['id' => $item->product_id])
    //             ->select(['unit'])
    //             ->scalar();
    //         if ($unit == 0) {
    //             $unit = 1;
    //         }
    //         $sum += ($set[$low_key] - $set[$high_key]) * $unit * $item['quantity'];
    //     }
    //
    //     return $sum;
    // }

    // 获取平级奖, 低推高
    // private function getEqualOrLow($key)
    // {
    //     $order_items = OrderItem::find()
    //         ->where(['order_id' => $this->order->id])
    //         ->all();
    //
    //     $sum = 0;
    //     foreach ($order_items as $item) {
    //         $set = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();
    //         $unit = Product::find()
    //             ->where(['id' => $item->product_id])
    //             ->select(['unit'])
    //             ->scalar();
    //         if ($unit == 0) {
    //             $unit = 1;
    //         }
    //         $sum += ($set[$key]) * $unit * $item['quantity'];
    //     }
    //
    //     return $sum;
    // }

    // protected function diff_new()
    // {
    //     $referrer = Chain::getReferrer($this->user->id, true);
    //     if ($referrer) {
    //
    //         $diff_amount = 0;
    //         $equal_amount = 0;
    //         $low_amount = 0;
    //
    //         // 会员复购
    //         if ($this->user->level == UserLevel::$keys['vip1']) {
    //             // 只有差价奖
    //             if ($referrer->level == UserLevel::$keys['vip2']) {
    //                 $diff_amount = $this->getDiffPrice('diff_vip2', 'diff_vip1');
    //             }
    //             if ($referrer->level == UserLevel::$keys['vip3']) {
    //                 $diff_amount = $this->getDiffPrice('diff_vip3', 'diff_vip1');
    //             }
    //             if ($referrer->level == UserLevel::$keys['vip4']) {
    //                 $diff_amount = $this->getDiffPrice('diff_vip4', 'diff_vip1');
    //             }
    //         }
    //
    //         // 区代复购
    //         if ($this->user->level == UserLevel::$keys['vip2']) {
    //             // 差价
    //             if ($referrer->level == UserLevel::$keys['vip3']) {
    //                 $diff_amount = $this->getDiffPrice('diff_vip3', 'diff_vip2');
    //             }
    //             if ($referrer->level == UserLevel::$keys['vip4']) {
    //                 $diff_amount = $this->getDiffPrice('diff_vip4', 'diff_vip2');
    //             }
    //
    //             // 平级
    //             if ($referrer->level == UserLevel::$keys['vip2']) {
    //                 $equal_amount = $this->getEqualOrLow('equal');
    //             }
    //
    //             // 低推高
    //             if ($referrer->level == UserLevel::$keys['vip1']) {
    //                 $equal_amount = $this->getEqualOrLow('low');
    //             }
    //         }
    //
    //         // 市代复购
    //         if ($this->user->level == UserLevel::$keys['vip3']) {
    //             // 差价
    //             if ($referrer->level == UserLevel::$keys['vip4']) {
    //                 $diff_amount = $this->getDiffPrice('diff_vip4', 'diff_vip3');
    //             }
    //
    //             // 平级
    //             if ($referrer->level == UserLevel::$keys['vip3']) {
    //                 $equal_amount = $this->getEqualOrLow('equal');
    //             }
    //
    //             // 低推高
    //             if ($referrer->level == UserLevel::$keys['vip1'] || $referrer->level == UserLevel::$keys['vip2']) {
    //                 $equal_amount = $this->getEqualOrLow('low');
    //             }
    //         }
    //
    //         if ($this->user->level == UserLevel::$keys['vip4']) {
    //             // 平级
    //             if ($referrer->level == UserLevel::$keys['vip4']) {
    //                 $equal_amount = $this->getEqualOrLow('equal');
    //             }
    //
    //             // 低推高
    //             if ($referrer->level == UserLevel::$keys['vip1'] || $referrer->level == UserLevel::$keys['vip2'] || $referrer->level == UserLevel::$keys['vip3']) {
    //                 $equal_amount = $this->getEqualOrLow('low');
    //             }
    //
    //             // var_dump($equal_amount);exit;
    //         }
    //
    //         if ($diff_amount > 0) {
    //             $this->saveCommission($referrer->id, $this->order->user_id, $this->order->id,
    //                 $diff_amount, '21', '复购差价');
    //         }
    //         if ($equal_amount > 0) {
    //             $this->saveCommission($referrer->id, $this->order->user_id, $this->order->id,
    //                 $equal_amount, '22', '复购平级奖');
    //         }
    //         if ($low_amount > 0) {
    //             $this->saveCommission($referrer->id, $this->order->user_id, $this->order->id,
    //                 $low_amount, '23', '复购低推高');
    //         }
    //
    //
    //     }
    // }

    // 复购差价然
    // protected function diff()
    // {
    //     $chain = Chain::getReferrerChain($this->order->user_id, [], true);
    //
    //     $award_name = '复购差价';
    //     $award_type = 12;
    //
    //     $diff_vip1 = 0;
    //     $diff_vip2 = 0;
    //     $diff_vip3 = 0;
    //     $diff_vip4 = 0;
    //
    //     $has_vip1 = false;
    //     $has_vip2 = false;
    //     $has_vip3 = false;
    //     $has_vip4 = false;
    //
    //     foreach ($chain as $user) {
    //         if ($user->level <= $this->user->level) {
    //             continue;
    //         }
    //
    //         // 去掉会员
    //         if ($user->level == UserLevel::$keys['vip1']) {
    //             continue;
    //         }
    //
    //         if ((!$has_vip1 && !$has_vip2 && !$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip1']) {
    //             $diff_vip1 = $this->_diff('diff_vip1');
    //             $this->saveCommission($user->id, $this->order->user_id, $this->order->id, $diff_vip1, $award_type, $award_name);
    //             $has_vip1 = true;
    //         }
    //         if ((!$has_vip2 && !$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip2']) {
    //             $diff_vip2 = $this->_diff('diff_vip2');
    //             $diff_vip2 -= $diff_vip1;
    //             $this->saveCommission($user->id, $this->order->user_id, $this->order->id, $diff_vip2, $award_type, $award_name);
    //             $has_vip2 = true;
    //         }
    //         if ((!$has_vip3 && !$has_vip4) && $user->level == UserLevel::$keys['vip3']) {
    //             $diff_vip3 = $this->_diff('diff_vip3');
    //             $diff_vip3 -= $diff_vip2 + $diff_vip1;
    //             $this->saveCommission($user->id, $this->order->user_id, $this->order->id, $diff_vip3, $award_type, $award_name);
    //             $has_vip3 = true;
    //         }
    //         if (!$has_vip4 && $user->level == UserLevel::$keys['vip4']) {
    //             $diff_vip4 = $this->_diff('diff_vip4');
    //             $diff_vip4 -= $diff_vip3 + $diff_vip2 + $diff_vip1;
    //             $this->saveCommission($user->id, $this->order->user_id, $this->order->id, $diff_vip4, $award_type, $award_name);
    //             $has_vip4 = true;
    //             break;
    //         }
    //     }
    // }

    // private function _diff($diff_key)
    // {
    //     $order_items = OrderItem::find()
    //         ->where(['order_id' => $this->order->id])
    //         ->all();
    //
    //     $sum = 0;
    //     foreach ($order_items as $item) {
    //         // 这里可以不用商品原价, order_item中是折后价
    //         $product = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();
    //         // var_dump($product);exit;
    //         $sum += ($item['price'] - $product[$diff_key]) * $item['quantity'];
    //     }
    //
    //     return $sum;
    // }

    // 推荐奖
    // protected function recommend()
    // {
    //     $total_price = $this->order->total_pay - $this->order->total_freight;
    //     $amount = 0;
    //
    //     $referrer = Chain::getReferrer($this->user->id, true);
    //     if ($referrer) {
    //
    //         // 区代
    //         if ($referrer->level == UserLevel::$keys['vip2']) {
    //             if ($total_price == $this->set->vip1) {
    //                 $amount = $this->set->vip2_vip1;
    //             }
    //             if ($total_price == $this->set->vip2) {
    //                 $amount = $this->set->vip2_vip2;
    //             }
    //             if ($total_price == $this->set->vip3) {
    //                 $amount = $this->set->vip2_vip3;
    //             }
    //             if ($total_price == $this->set->vip4) {
    //                 $amount = $this->set->vip2_vip4;
    //             }
    //         }
    //
    //         // 市代
    //         if ($referrer->level == UserLevel::$keys['vip3']) {
    //             if ($total_price == $this->set->vip1) {
    //                 $amount = $this->set->vip3_vip1;
    //             }
    //             if ($total_price == $this->set->vip2) {
    //                 $amount = $this->set->vip3_vip2;
    //             }
    //             if ($total_price == $this->set->vip3) {
    //                 $amount = $this->set->vip3_vip3;
    //             }
    //             if ($total_price == $this->set->vip4) {
    //                 $amount = $this->set->vip3_vip4;
    //             }
    //         }
    //
    //         // 省代
    //         if ($referrer->level == UserLevel::$keys['vip4']) {
    //             if ($total_price == $this->set->vip1) {
    //                 $amount = $this->set->vip4_vip1;
    //             }
    //             if ($total_price == $this->set->vip2) {
    //                 $amount = $this->set->vip4_vip2;
    //             }
    //             if ($total_price == $this->set->vip3) {
    //                 $amount = $this->set->vip4_vip3;
    //             }
    //             if ($total_price == $this->set->vip4) {
    //                 $amount = $this->set->vip4_vip4;
    //             }
    //         }
    //
    //         $this->saveCommission($referrer->id, $this->order->user_id, $this->order->id,
    //             $amount, '11', '推荐奖');
    //
    //
    //     }
    //
    //
    // }

    // 销售分红奖
    // protected function sell(User $referrer)
    // {
    //     // var_dump($referrer);exit;
    //
    //     // 直推多少个区代+
    //     $children = User::find()
    //         ->where(['referrer_id' => $referrer->id])
    //         ->andWhere(['level' => [
    //             UserLevel::$keys['vip2'],
    //             UserLevel::$keys['vip3'],
    //             UserLevel::$keys['vip4']
    //         ]])
    //         ->select(['id'])
    //         ->column();
    //
    //     // 小于5个, 则不返
    //     if (count($children) < $this->set->sell_vip) {
    //         return;
    //     }
    //
    //     // var_dump($referrer);exit;
    //
    //
    //     // 推荐人业绩
    //     $unit = $this->getUnit($referrer->id);
    //
    //     // var_dump($referrer->id, $unit);exit;
    //
    //     foreach ($children as $id) {
    //
    //         // var_dump($this->set->sell_num * $unit / 100, $unit);exit;
    //
    //         // 如果团队中存在大于50%业绩的人, 则不算
    //         $u = $this->getUnit($id);
    //         if ($u >= $this->set->sell_num * $unit / 100) {
    //             $unit -= $u;
    //         }
    //     }
    //
    //     // 去掉已返的
    //     // $unit = $this->subUnit($referrer, $unit);
    //     //
    //     // $amount = 0;
    //     // if ($unit >= $this->set->sell_4) {
    //     //     $amount = $this->set->sell_4_amount;
    //     //     $referrer->sell_4 = $referrer->sell_4 + 1;
    //     // } else if ($unit >= $this->set->sell_3) {
    //     //     $amount = $this->set->sell_3_amount;
    //     //     $referrer->sell_3 = $referrer->sell_3 + 1;
    //     // } else if ($unit >= $this->set->sell_2) {
    //     //     $amount = $this->set->sell_2_amount;
    //     //     $referrer->sell_2 = $referrer->sell_2 + 1;
    //     // } else if ($unit >= $this->set->sell_1) {
    //     //     $amount = $this->set->sell_1_amount;
    //     //     $referrer->sell_1 = $referrer->sell_1 + 1;
    //     // }
    //
    //     // var_dump($unit);exit;
    //
    //     // 改成只返一次了
    //     $amount = 0;
    //
    //     if ($unit >= $this->set->sell_1 && $referrer->sell_1 <= 0) {
    //         $amount = $this->set->sell_1_amount;
    //         $referrer->sell_1 = $referrer->sell_1 + 1;
    //     } else if ($unit >= $this->set->sell_2 && $referrer->sell_2 <= 0) {
    //         $amount = $this->set->sell_2_amount;
    //         $referrer->sell_2 = $referrer->sell_2 + 1;
    //     } else if ($unit >= $this->set->sell_3 && $referrer->sell_3 <= 0) {
    //         $amount = $this->set->sell_3_amount;
    //         $referrer->sell_3 = $referrer->sell_3 + 1;
    //     } else if ($unit >= $this->set->sell_4 && $referrer->sell_4 <= 0) {
    //         $amount = $this->set->sell_4_amount;
    //         $referrer->sell_4 = $referrer->sell_4 + 1;
    //     }
    //
    //     if ($amount > 0) {
    //         $referrer->save();
    //         $this->saveCommission($referrer->id, $this->order->user_id, $this->order->id,
    //             $amount, '30', '分红');
    //     }
    // }

    // 判断销售分红
    // private function subUnit(User $user, $unit)
    // {
    //     $sell4 = $user->sell_4 * $this->set->sell_4;
    //     $sell3 = $user->sell_3 * $this->set->sell_3;
    //     $sell2 = $user->sell_2 * $this->set->sell_2;
    //     $sell1 = $user->sell_1 * $this->set->sell_1;
    //
    //     $total = $unit - $sell4 - $sell3 - $sell2 - $sell1;
    //
    //     // var_dump($total);exit;
    //
    //     if ($total < 0) {
    //         $total = 0;
    //     }
    //
    //     return $total;
    // }

    // 业绩 (多少套), 团队业绩
    // private function getUnit($user_id)
    // {
    //     $ids = Chain::getChildrenAllQuick($user_id, [
    //         UserLevel::$keys['vip2'],
    //         UserLevel::$keys['vip3'],
    //         UserLevel::$keys['vip4']
    //     ], ['id']);
    //     $ids = ArrayHelper::getColumn($ids, 'id');
    //     $ids = array_merge([$user_id], $ids);
    //
    //     // var_dump($ids);exit;
    //
    //     $orders = Order::find()
    //         ->where(['user_id' => $ids])
    //         ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
    //         ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
    //         ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
    //         ->with(['item.product'])
    //         ->select(['id'])
    //         ->asArray()
    //         ->all();
    //
    //     // var_dump($orders);exit;
    //
    //     // 计算产品套数
    //     $sum = 0;
    //     foreach ($orders as $order) {
    //         foreach ($order['item'] as $item) {
    //             $box = $item['product']['unit'];
    //             $sum += $box * $item['quantity'];
    //         }
    //     }
    //
    //     return $sum;
    // }

    // 保存佣金
    protected function saveCommission($user_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->order->user_id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = 1;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {

                // balance logs
                UserBalance::addLog($user_id,
                    $account->balance, $balance, $account->balance + $balance, '返佣', $this->order->id);


                $account->balance += $balance;
                $account->save();
            }
        }
    }
}